package cn.shper.skana.exercise;

import java.util.List;
import java.util.Random;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Text;
import org.eclipse.wb.swt.SWTResourceManager;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.events.SelectionAdapter;

public class ExerciseFrame extends Shell {
	private Text input_txt;
	private CLabel show_lbl;
	private CLabel answer_lbl;
	private Button kanaroma_rdbtn;
	private Button romakana_rdbtn;
	private Label seion_lbl;
	private Label dakuon_lbl;
	private Label youon_lbl;
	private Group group;
	private Button seionkata_chckbtn;
	private Button seionhira_chckbtn;
	private Button dakuonhira_chckbtn;
	private Button dakuonkata_chckbtn;
	private Button youonhira_chckbtn;
	private Button youonkata_chckbtn;
	private CLabel val_lbl;
	private Button okbtn;
	private Button next_btn;
	private Button answer_btn;
	private Label kanalbl;
	private int i=0;
	private boolean[] checked = new boolean[6];
	private List<String[]> kanaList;
	private CheckBoxAction checkBox_Action = new CheckBoxAction();
	private String[] shows;
	private String[] answers;
	private SearchKana searchKana = new SearchKana();
	
	/**
	 * Create the shell.
	 * @param display
	 */
	public ExerciseFrame(Display display) {
		super(display, SWT.CLOSE | SWT.MIN | SWT.TITLE);
		setLayout(null);
		createControls();
		createActionListener();
	}
	
	/**
	 * Create Controls of the shell.
	 */
	protected void createControls(){
		setSize(490, 287);
		setText("仮名練習  V2.3 By Shper");
		
		kanalbl = new Label(this, SWT.CENTER);
		kanalbl.setBounds(205, 36, 74, 25);
		kanalbl.setFont(SWTResourceManager.getFont("微软雅黑", 12, SWT.NORMAL));
		kanalbl.setText("(平)あ行");
		
		input_txt = new Text(this, SWT.BORDER);
		input_txt.setFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.NORMAL));
		input_txt.setBounds(53, 228, 47, 23);
		
		val_lbl = new CLabel(this, SWT.CENTER);
		val_lbl.setFont(SWTResourceManager.getFont("微软雅黑", 16, SWT.BOLD));
		val_lbl.setBounds(103, 224, 26, 27);
		
		okbtn = new Button(this, SWT.NONE);
		okbtn.setFont(SWTResourceManager.getFont("微软雅黑", 9, SWT.NORMAL));
		okbtn.setBounds(150, 227, 80, 27);
		okbtn.setText("はい");
		
		next_btn = new Button(this, SWT.NONE);
		next_btn.setFont(SWTResourceManager.getFont("微软雅黑", 9, SWT.NORMAL));
		next_btn.setBounds(271, 225, 80, 27);
		next_btn.setText("次へ");
		
		answer_btn = new Button(this, SWT.NONE);
		answer_btn.setFont(SWTResourceManager.getFont("微软雅黑", 9, SWT.NORMAL));
		answer_btn.setBounds(387, 225, 80, 27);
		answer_btn.setText("答案");
		
		answer_lbl = new CLabel(this, SWT.CENTER);
		answer_lbl.setFont(SWTResourceManager.getFont("微软雅黑", 31, SWT.NORMAL));
		answer_lbl.setBounds(47, 1, 190, 58);
		answer_lbl.setText("kyu");
		
		group = new Group(this, SWT.NONE);
		group.setText("設置");
		group.setBounds(283, 36, 189, 163);
		
		kanaroma_rdbtn = new Button(group, SWT.RADIO);
		kanaroma_rdbtn.setSelection(true);
		kanaroma_rdbtn.setBounds(10, 30, 170, 20);
		kanaroma_rdbtn.setFont(SWTResourceManager.getFont("微软雅黑", 9, SWT.NORMAL));
		kanaroma_rdbtn.setText("仮名 -> ローマ");
		
		romakana_rdbtn = new Button(group, SWT.RADIO);
		romakana_rdbtn.setBounds(10, 55, 170, 20);
		romakana_rdbtn.setFont(SWTResourceManager.getFont("微软雅黑", 9, SWT.NORMAL));
		romakana_rdbtn.setText("ローマ -> 仮名");
		
		seion_lbl = new Label(group, SWT.NONE);
		seion_lbl.setBounds(10, 86, 37, 20);
		seion_lbl.setFont(SWTResourceManager.getFont("微软雅黑", 9, SWT.NORMAL));
		seion_lbl.setText("清音");
		
		dakuon_lbl = new Label(group, SWT.NONE);
		dakuon_lbl.setBounds(10, 111, 37, 20);
		dakuon_lbl.setFont(SWTResourceManager.getFont("微软雅黑", 9, SWT.NORMAL));
		dakuon_lbl.setText("濁音");
		
		youon_lbl = new Label(group, SWT.NONE);
		youon_lbl.setBounds(10, 134, 37, 20);
		youon_lbl.setFont(SWTResourceManager.getFont("微软雅黑", 9, SWT.NORMAL));
		youon_lbl.setText("拗音");
		
		youonhira_chckbtn = new Button(group, SWT.CHECK);
		youonhira_chckbtn.setBounds(52, 134, 65, 20);
		youonhira_chckbtn.setFont(SWTResourceManager.getFont("微软雅黑", 9, SWT.NORMAL));
		youonhira_chckbtn.setText("平仮名");
		
		dakuonhira_chckbtn = new Button(group, SWT.CHECK);
		dakuonhira_chckbtn.setBounds(52, 111, 65, 20);
		dakuonhira_chckbtn.setFont(SWTResourceManager.getFont("微软雅黑", 9, SWT.NORMAL));
		dakuonhira_chckbtn.setText("平仮名");
		
		seionhira_chckbtn = new Button(group, SWT.CHECK);
		seionhira_chckbtn.setBounds(52, 86, 65, 20);
		seionhira_chckbtn.setFont(SWTResourceManager.getFont("微软雅黑", 9, SWT.NORMAL));
		seionhira_chckbtn.setText("平仮名");
		
		seionkata_chckbtn = new Button(group, SWT.CHECK);
		seionkata_chckbtn.setBounds(120, 86, 65, 20);
		seionkata_chckbtn.setFont(SWTResourceManager.getFont("微软雅黑", 9, SWT.NORMAL));
		seionkata_chckbtn.setText("片仮名");
		
		dakuonkata_chckbtn = new Button(group, SWT.CHECK);
		dakuonkata_chckbtn.setBounds(120, 111, 65, 20);
		dakuonkata_chckbtn.setFont(SWTResourceManager.getFont("微软雅黑", 9, SWT.NORMAL));
		dakuonkata_chckbtn.setText("片仮名");
		
		youonkata_chckbtn = new Button(group, SWT.CHECK);
		youonkata_chckbtn.setBounds(120, 134, 65, 20);
		youonkata_chckbtn.setFont(SWTResourceManager.getFont("微软雅黑", 9, SWT.NORMAL));
		youonkata_chckbtn.setText("片仮名");
		
		show_lbl = new CLabel(this, SWT.CENTER);
		show_lbl.setFont(SWTResourceManager.getFont("微软雅黑", 99, SWT.NORMAL));
		show_lbl.setBounds(7, 36, 270, 178);
		show_lbl.setText("きゅ");
	}
	
	@Override
	protected void checkSubclass() {
		
	}	
	
	protected void createActionListener(){
		seionhira_chckbtn.addSelectionListener(chckBtnListenner);
		seionkata_chckbtn.addSelectionListener(chckBtnListenner);
		dakuonhira_chckbtn.addSelectionListener(chckBtnListenner);
		dakuonkata_chckbtn.addSelectionListener(chckBtnListenner);
		youonhira_chckbtn.addSelectionListener(chckBtnListenner);
		youonkata_chckbtn.addSelectionListener(chckBtnListenner);
		kanaroma_rdbtn.addSelectionListener(radioBtnListener);
		answer_btn.addSelectionListener(answerBtnListenner);
		okbtn.addSelectionListener(okbtnListenner);
		next_btn.addSelectionListener(nextBtnListener);
	}
	
	// Check 事件
	private SelectionListener chckBtnListenner = new SelectionAdapter() {
		@Override
		public void widgetSelected(SelectionEvent e) {

			checked[0] = seionhira_chckbtn.getSelection();
			checked[1] = seionkata_chckbtn.getSelection();

			checked[2] = dakuonhira_chckbtn.getSelection();
			checked[3] = dakuonkata_chckbtn.getSelection();

			checked[4] = youonhira_chckbtn.getSelection();
			checked[5] = youonkata_chckbtn.getSelection();
			kanaList = checkBox_Action.addKana(checked);

			isRadioBtnSelect();

			for (boolean i : checked) {
				if (i == true) {
					next_btn.setEnabled(true);
					answer_btn.setEnabled(false);
					okbtn.setEnabled(false);
					input_txt.setEnabled(false);
					return;
				}
			}

			if (checked[0] == false && checked[1] == false && checked[2] == false
					&& checked[3] == false && checked[4] == false && checked[4] == false) {
				next_btn.setEnabled(false);
				answer_btn.setEnabled(false);
				okbtn.setEnabled(false);
				input_txt.setEnabled(false);
				// answer_lbl.setText("a");
				// kana_lbl.setText("あ");
			}
		}
	};
	
	// Answer 事件
	private SelectionListener answerBtnListenner = new SelectionAdapter() {
		public void widgetSelected(SelectionEvent e) {
			answer_lbl.setText(answers[i]);
		}
	};
	
	// Next 事件
	private SelectionListener nextBtnListener = new SelectionAdapter() {

		@Override
		public void widgetSelected(SelectionEvent e) {
			Random r = new Random();
			answer_lbl.setText("");
			val_lbl.setText("");
			input_txt.setText("");
			i = r.nextInt(shows.length - 1);
			String kana = shows[i];
			show_lbl.setText(kana);
			answer_btn.setEnabled(true);
			okbtn.setEnabled(true);
			input_txt.setEnabled(true);
			if (romakana_rdbtn.getSelection()) {
				 kanalbl.setText(searchKana.search(answers[i]));
			}
		}
	};
	
	//  事件
	
	private SelectionListener radioBtnListener = new SelectionAdapter() {
		@Override
		public void widgetSelected(SelectionEvent e) {
			 isRadioBtnSelect();
		}
	};
	
	private void isRadioBtnSelect() {
		if (kanaList != null) {
			if (kanaroma_rdbtn.getSelection()) {
				kanalbl.setText("");
				shows = kanaList.get(0);
				answers = kanaList.get(1);
			}
			if (romakana_rdbtn.getSelection()) {
				answers = kanaList.get(0);
				shows = kanaList.get(1);
			}
		}
	}
	
	// OK 事件
	private SelectionListener okbtnListenner = new SelectionAdapter() {

		@Override
		public void widgetSelected(SelectionEvent e) {
			if (answers[i].equals(input_txt.getText())) {
				val_lbl.setText("√");
				answer_lbl.setText(answers[i]);
				val_lbl.setForeground(SWTResourceManager.getColor(SWT.COLOR_GREEN));
			} else {
				val_lbl.setText("×");
				val_lbl.setForeground(SWTResourceManager.getColor(SWT.COLOR_RED));
			}
		}
	};
	
}
